"use strict";
const mongoose = require('mongoose');
const path =  require('path');
const Project = require('project');
const {update} = require('./dbMethod');
const Schema = mongoose.Schema;

const schema = new Schema({
    name: {type:String, required:true},
    imgUrl: String,
    desc: String,
    updateTime: { type:Date, default:Date.now },
    createTime: { type:Date, default:Date.now },
    enabled: {type:Boolean, default:true}
});

schema.statics.add = async ( json ) => {
    try{
        let guest = new Model( json );
        const ret = await guest.save();
        return ret;
    }catch(err){
        throw {code:102, error:'Add guest failed.'};
    }
}

schema.methods.update = async ( json,doc ) => {
    const editableFields = ['name','imgUrl','desc'];
    return update(json,doc,editableFields);
}

// schema.methods.findDetail = async (guest) => {
//     const _this = guest;
//     const projects = Project.find({guestId:_this._id});
//     return Object.assign(_this,{projects:projects});
// }

const Model = exports.model = mongoose.model( path.basename(__filename,'.js');